using System;
using UnityEngine;

[Serializable]
public struct LayerTimeline
{
	public string name;

	public KeyframeObjectTransform[] frames;

	public AnimationCurve curveRotation;

	public AnimationCurve curveX;

	public AnimationCurve curveY;

	public AnimationCurve curveSprite;

	public LayerTimeline(string layerName)
	{
		name = layerName;
		frames = new KeyframeObjectTransform[0];
		curveRotation = new AnimationCurve();
		curveX = new AnimationCurve();
		curveY = new AnimationCurve();
		curveSprite = new AnimationCurve();
	}

	public void UpdateCurve(int frameRate, float pixelPerUnit, LibrarySprites[] library)
	{
		if (frames.Length == 0)
		{
			UnityEngine.Debug.LogWarning("LayerTimeline: пустой список кадров в слое <" + name + ">");
			return;
		}
		float num = 0f;
		curveRotation.keys = new Keyframe[0];
		curveX.keys = new Keyframe[0];
		curveY.keys = new Keyframe[0];
		curveSprite.keys = new Keyframe[0];
		int num2 = 0;
		for (num2 = 0; num2 < frames.Length; num2++)
		{
			num = Round((float)frames[num2].frame / (float)frameRate);
			curveRotation.AddKey(num, Round(frames[num2].rotation));
			curveX.AddKey(num, Round(frames[num2].x / pixelPerUnit));
			curveY.AddKey(num, Round(frames[num2].y / pixelPerUnit));
		}
		int frame = frames[frames.Length - 1].frame;
		int num3 = 0;
		int num4 = 0;
		KeyframeObjectTransform keyframeObjectTransform = new KeyframeObjectTransform(0, 0f, 0f, 0f);
		for (int i = 0; i <= frame; i++)
		{
			for (num4 = 0; num4 < frames.Length; num4++)
			{
				if (i == frames[num4].frame)
				{
					keyframeObjectTransform = frames[num4];
					num3 = keyframeObjectTransform.frameSprite;
					break;
				}
			}
			if (keyframeObjectTransform.loop == KeyframeObjectTransform.ModeLoop.Loop && i != keyframeObjectTransform.frame)
			{
				num3++;
				int num5 = num3;
				LibrarySprites librarySprite = GetLibrarySprite(keyframeObjectTransform.libraryName, library);
				num3 = num5 % librarySprite.sprites.Length;
			}
			else if (keyframeObjectTransform.loop == KeyframeObjectTransform.ModeLoop.PlayOnce && i != keyframeObjectTransform.frame)
			{
				num3++;
				int a = num3;
				LibrarySprites librarySprite2 = GetLibrarySprite(keyframeObjectTransform.libraryName, library);
				num3 = Mathf.Min(a, librarySprite2.sprites.Length - 1);
			}
			curveSprite.AddKey(Round((float)i / (float)frameRate), num3);
		}
	}

	public static LibrarySprites GetLibrarySprite(string name, LibrarySprites[] listSprites)
	{
		for (int i = 0; i < listSprites.Length; i++)
		{
			if (listSprites[i].name == name)
			{
				return listSprites[i];
			}
		}
		return default(LibrarySprites);
	}

	public static float Round(float value)
	{
		return Mathf.Round(value * 1000f) / 1000f;
	}

	public static float Floor(float value)
	{
		return Mathf.Floor(value * 1000f) / 1000f;
	}
}
